package com.hrhx.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.hrhx.bean.ChinaAreaDataBean;
import com.hrhx.dao.ChinaAreaDataDao;
import com.hrhx.util.StringUtil;
@WebServlet(name = "ChinaAreaDataServlet", urlPatterns = { "/ChinaAreaData" }, loadOnStartup = 1)
public class ChinaAreaDataServlet extends HttpServlet {
	
	private static final long serialVersionUID = -6886697421555222670L;
	private ChinaAreaDataDao chinaAreaDataDao= new ChinaAreaDataDao();
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		this.doPost(request, response);
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//获取请求次数
	    String draw = "0";
	    //总记录数
	    long recordsTotal = 0;
	    //过滤后记录数
	    long recordsFiltered = 0;
	    
	    draw = request.getParameter("draw");
	    //数据起始位置
	    String start = request.getParameter("start");
	    //数据长度
	    String length = request.getParameter("length");
	    
	    //定义列名
	    String[] cols = {"id", "name", "short_name", "merger_name", "pinyin", "level", "zip_code", "city_code", "area_code", "lng", "lat"};
	    //获取客户端需要那一列排序
	    String orderColumn = "0";
	    orderColumn = request.getParameter("order[0][column]");
	    orderColumn = cols[Integer.parseInt(orderColumn)];
	    //获取排序方式 默认为asc
	    String orderDir = "asc";
	    orderDir = request.getParameter("order[0][dir]");
	    
	    //获取用户过滤框里的字符
	    String searchValue = request.getParameter("search[value]");

	    List<String> sArray = new ArrayList<String>();
	    if (!searchValue.equals("")) {
	    	//sArray.add(" id like '%" + searchValue + "%'");
	        sArray.add(" name like '%" + searchValue + "%'");
	        sArray.add(" short_name like '%" + searchValue + "%'");
	        sArray.add(" merger_name like '%" + searchValue + "%'");
	        sArray.add(" pinyin like '%" + searchValue + "%'");
	        //sArray.add(" level like '%" + searchValue + "%'");
	        sArray.add(" zip_code like '%" + searchValue + "%'");
	        sArray.add(" city_code like '%" + searchValue + "%'");
	        sArray.add(" area_code like '%" + searchValue + "%'");
	        sArray.add(" lng like '%" + searchValue + "%'");
	        sArray.add(" lat like '%" + searchValue + "%'");
	    }

	    String individualSearch = "";
	    if (sArray.size() == 1) {
	        individualSearch = sArray.get(0);
	    } else if (sArray.size() > 1) {
	        for (int i = 0; i < sArray.size() - 1; i++) {
	            individualSearch += sArray.get(i) + " or ";
	        }
	        individualSearch += sArray.get(sArray.size() - 1);
	    }
	    
	    String searchSQL = "";
        String sql = "SELECT * FROM lkk_cnarea";
        String recordsFilteredSql = "select count(1) as recordsFiltered from lkk_cnarea";
        if (individualSearch != "") {
            searchSQL = " where " + individualSearch;
        }
        sql += searchSQL;
        recordsFilteredSql += searchSQL;
        sql += " order by " + orderColumn + " " + orderDir;
        recordsFilteredSql += " order by " + orderColumn + " " + orderDir;
        sql += " limit " + start + ", " + length;
        
	    System.out.println(sql);
        List<ChinaAreaDataBean> chinaAreaDatas= chinaAreaDataDao.getAll(sql);
		recordsTotal = chinaAreaDataDao.getRecordsTotal();
		recordsFiltered = chinaAreaDataDao.getRecordsFiltered(recordsFilteredSql);
		Map<Object, Object> info = new HashMap<Object, Object>();
	    info.put("data", chinaAreaDatas);
	    info.put("recordsTotal", recordsTotal);
	    info.put("recordsFiltered", recordsFiltered);
	    info.put("draw", draw);
	    String json = new Gson().toJson(info);
	    StringUtil.outPageByJson(response, json);
	}
}
